package com.cms.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.action.DefaultAction;
import com.cms.model.ImportLog;
import com.cms.service.ImportLogService;
import com.cms.service.ImportLogServiceImpl;
import com.util.Config;
import com.util.ConfigUtil;
import com.util.Util;

public class ImportDataLogQueryAction extends DefaultAction {
	List<ImportLog> list;
	String startdate;
	String enddate;
	ImportLogService service = new ImportLogServiceImpl();
	String refilename;
	String filename;
	public String query()
	{
		if(startdate==null || "".equals(startdate))
		{
			startdate = Util.dateToString(new Date(), "yyyy-MM")+"-01";
		}
		if(enddate==null || "".equals(enddate))
		{ 
			enddate = Util.dateToString(new Date(), "yyyy-MM-dd");
		}
		list = service.getImportLog(startdate, enddate);
		return SUCCESS;
	}
	public String d()
	{
		try {
			HttpServletResponse response = super.getResponse();
			InputStream is = new BufferedInputStream(new FileInputStream(ConfigUtil.get(Config.SECTIONMANAGE, Config.KEY_IMPORTDATA_SAVE_PATH) + 
					"/"+refilename));
			byte[] b = new byte[is.available()];
			is.read(b);
			is.close();
			
			String fileName = filename;
			
	    	response.setContentType("application/octet-stream");
	    	response.setHeader("Location", fileName);
	    	response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
	    	OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
	    	outputStream.write(b);
	    	outputStream.flush();
	    	outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public List<ImportLog> getList() {
		return list;
	}
	public void setList(List<ImportLog> list) {
		this.list = list;
	}
	public String getStartdate() {
		return startdate;
	}
	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}
	public String getEnddate() {
		return enddate;
	}
	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}
	public String getRefilename() {
		return refilename;
	}
	public void setRefilename(String refilename) {
		this.refilename = refilename;
	}
	public String getFilename() {
		return filename;
	}
	public void setFilename(String filename) {
		this.filename = filename;
	}
	
}
